package middlewares

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"github.com/spf13/cast"
	"learn/app/controllers/cc"
)

func LogRequest() gin.HandlerFunc {
	return func(c *gin.Context) {

		res_map := make(map[string]interface{}, 5)
		//fmt.Println(c.Request.Method)
		if c.Request.Method == "POST" {
			mapp := make(map[string]interface{}, 100)
			if err := c.ShouldBindBodyWith(&mapp, binding.JSON); err != nil {
				cc.Return_fail(c, cc.WithMsg("解析错误"))
				return
			}
			marshal11, _ := json.Marshal(mapp)
			res_map["msg"] = cast.ToString(marshal11)
		} else {
			map_str := ""
			map_str = c.Request.URL.RawQuery
			res_map["msg"] = map_str
		}

		// 处理请求
		c.Next()
	}
}
